/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import org.jfonia.connect5.basics.BasicValueNode;
import org.jfonia.connect5.basics.MutableValueNode;
import org.jfonia.constants.ViewConstants;
import org.jfonia.images.ImageConstants;
import org.jfonia.images.ScaledImageIcon;
import org.jfonia.language.LabelConstants;
import org.jfonia.language.LanguageResource;
import org.jfonia.model.StaffCollection;
import percussiongenerator.gui.TappingPanel;
import percussiongenerator.model.Track;
import percussiongenerator.model.TrackCollection;

/**
 *
 * @author Rik Bauwens
 */
public class TappingDialog extends JDialog
{
        private static TappingDialog instance;

        private TrackCollection trackCollection;

        private TappingDialog()
        {
                super();
                setVisible(false);
                setModal(true);
                setLocationRelativeTo(null);
                setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
                setTitle(LanguageResource.getInstance().getLabel(LabelConstants.RHYTHM_BY_TAPPING));
                setIconImage(new ScaledImageIcon(ImageConstants.KEYS, "").
                        setMaximumSide(ImageConstants.FRAME_ICON_SIZE).getImageIcon().getImage());
                getContentPane().setLayout(new BorderLayout());

                trackCollection = new TrackCollection(LeadSheetPlayer.getInstance().getMidiPlayer());
                MutableValueNode node = new BasicValueNode(trackCollection);
                TappingPanel tappingPanel = new TappingPanel(trackCollection, node);
                getContentPane().add(tappingPanel, BorderLayout.CENTER);

                JButton createLeadSheetButton = new JButton(LanguageResource.getInstance()
                        .getLabel(LabelConstants.CREATE_LEADSHEET));
                createLeadSheetButton.addActionListener(new ActionListener()
                {
                        public void actionPerformed(ActionEvent e)
                        {
                                LeadSheet leadSheet = new LeadSheet();
                                StaffCollection staffCollection = new StaffCollection();
                                for(Track track : trackCollection.getTracks())
                                        staffCollection.addStaff(track.getStaff());
                                leadSheet.addStaffCollection(staffCollection);
                                Project.getInstance().setLeadSheet(null, leadSheet);
                                setVisible(false);
                        }
                });
                
                getContentPane().add(createLeadSheetButton, BorderLayout.SOUTH);
                
                pack();
                setSize(new Dimension(ViewConstants.TAPPING_DIALOG_WIDTH, getHeight()));
                createLeadSheetButton.requestFocus();
        }

        public static synchronized TappingDialog getInstance()
        {
                if(instance == null)
                        instance = new TappingDialog();
                return instance;
        }

        public void startTapping()
        {
                trackCollection.removeAllTracks();
                setVisible(true);
        }

}
